<?php

use backend\Backend;
use backend\components\RbacPhpManager;
use common\CMS;
use yii\helpers\ArrayHelper;

$config = require Yii::getAlias('@common/config/main.php');

// 合并配置信息
foreach (['main', 'backend'] as $name)
{
    $config = ArrayHelper::merge($config, CMS::globModuleConfig($name, ['common', 'backend']));
}

$config = ArrayHelper::merge($config, [
    'id' => 'app-cms-backend',
    'name' => '后台系统',
    'basePath' => CMS_ROOT . 'backend' . DIRECTORY_SEPARATOR,
    'runtimePath' => CMS_ROOT . 'backend' . DIRECTORY_SEPARATOR . 'runtime',
    'controllerNamespace' => 'backend\\controllers',
    'defaultRoute'        => 'admin/default/index',
    'modules' => [
        // 系统相关模块
        'setting' => [
            'class' => 'funson86\setting\Module',
            'controllerNamespace' => 'funson86\setting\controllers'
        ],

        // 界面相关的模块
        'gridview'=>[
            'class'=>'kartik\grid\Module',
            // other module settings
        ],
        'treemanager' =>  [
            'class' => '\kartik\tree\Module',
            // other module settings, refer detailed documentation
        ],
    ],
    'components' => [
        'request' => [
            'cookieValidationKey' => 'Djflj3*dfs',
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                ''               => 'admin/default/index',
                Backend::$backendDir . '<controller:\w+>/<action:[\w-]+>/<id:\d+>' => '<controller>/<action>',
                Backend::$backendDir . '<module:\w+>/<controller:\w+>/<action:[\w-]+>/<id:\d+>' => '<module>/<controller>/<action>'
            ],
        ],
        'i18n' => [
            'translations' => [
                'backend' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'sourceLanguage' => 'en-US',
                    'basePath' => '@backend/messages',
                    'fileMap' => [
                        'backend' => 'backend.php',
                    ]
                ],
                'kvtree' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'sourceLanguage' => 'en-US',
                    'basePath' => '@common/messages',
                    'fileMap' => [
                        'kvtree' => 'kvtree.php',
                    ]
                ],
            ],
        ],
        'formatter' => [
            'sizeFormatBase' => 1000
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'view'                => [
            'theme' => 'backend\themes\Theme'
        ],
        'errorHandler' => [
            //'class' => '\bedezign\yii2\audit\components\web\ErrorHandler',
            'errorAction' => 'admin/default/error',
        ],
        'assetManager' => [
            'baseUrl' => 'http://'.$_SERVER['HTTP_HOST'].'/assets',
            'linkAssets' => true,
        ],
        'authManager' => [
            'class' => RbacPhpManager::className(),
            'defaultRoles' => [
                'user'
            ],
            'itemFile' => '@backend/config/rbac/items.php',
            'assignmentFile' => '@backend/config/rbac/assignments.php',
            'ruleFile' => '@backend/config/rbac/rules.php',
        ],
        "adminAccessLog"=>'backend\components\AdminAccessLog',
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
                //adminAccessLog
                [
                    'class' => 'yii\log\FileTarget',
                    'logFile' => "@runtime/logs/admin-access.log",
                    'levels' => ['info'],
                    'categories' => ['adminAccessLog'],
                    'logVars' => [],
                ],
            ],
        ],
        'sidebarMenuProvider' => [
            'class' => 'backend\components\SidebarMenuProvider',
            'itemDataPath'=>'@backend/config/menu/menu.php'
        ],
        "adminAuth"=>'backend\components\AdminAuth',
        "appMetaData"=> 'backend\components\AppMetaData',
    ],
    'bootstrap' => [
        'log',
        'backend\Bootstrap',
    ],
    'extensions' => require(CMS_ROOT . 'vendor/yiisoft/extensions.php'),
]);

// audit模块有点问题,暂时禁用
if (isset($config['extensions']['bedezign/yii2-audit']))
{
    $config['extensions']['bedezign/yii2-audit']['bootstrap'] = null;
}

if (is_file(__DIR__ . '/main-local.php'))
{
    $config = ArrayHelper::merge($config, @require __DIR__ . '/main-local.php');
}

return $config;
